import constant from './constant'

// 存储变量名
let storageKey = 'vita'

// 存储节点变量名
let storageKeys = Object.keys(constant).map(key => {
  return constant[key]
})

// 存储的数据
let storageData = uni.getStorageSync(storageKey) || {}

const storage = {
  set: function(key, value) {
    if (storageKeys.indexOf(key) != -1) {
      let tmp = uni.getStorageSync(storageKey)
      tmp = tmp ? tmp : {}
      tmp[key] = value
      storageData[storageKey] = value
      uni.setStorageSync(storageKey, tmp)
    }
  },
  get: function(key) {
    return storageData[key] || ""
  },
  remove: function(key) {
    delete storageData[key]
    uni.setStorageSync(storageKey, storageData)
  },
  clean: function() {
    uni.removeStorageSync(storageKey)
  }
}

export default storage

export {
  storageData,
	storageKey,
  storageKeys
}
